/**
 * 
 */
package com.xac.aas.logic;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;

import android.util.Log;

import com.xac.aas.model.UserInfo;
import com.xac.common.data.DataManager;

/**
 * @author daisongwan
 * 
 */
public class LoginFlowManager {
	private static final LoginFlowManager instance = new LoginFlowManager();
	private LoginFlowManager(){
		
	}
	// TODO Maybe should use singleton.
	public static LoginFlowManager getInstance() {
		return instance;
	}

	public boolean login(UserInfo userInfo) {
		boolean result = true;

		Log.d("LoginFlowManager", "LoginActivity:login incoked!");
		ConnectionConfiguration config = new ConnectionConfiguration(
				userInfo.getLoginServerIP(), userInfo.getLoginServerPort());
		config.setCompressionEnabled(false);
		config.setSASLAuthenticationEnabled(false);
		XMPPConnection connection = new XMPPConnection(config);
		try {
			Log.d("LoginFlowManager", "before connect.");
			connection.connect();
			connection.login(userInfo.getUsername(), userInfo.getPassword());
			Log.d("LoginFlowManager", "login successful!");
			DataManager.getInstance().setConnection(connection);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
			if(connection.isConnected()){
				connection.disconnect();
			}
		}
		return result;
	}
}
